package com.mtons.mblog.web.controller.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mtons.mblog.base.lang.Consts;
import com.mtons.mblog.base.lang.Result;
import com.mtons.mblog.modules.data.NavTypeVO;
import com.mtons.mblog.modules.data.NavVO;
import com.mtons.mblog.modules.data.PostVO;
import com.mtons.mblog.modules.entity.Channel;
import com.mtons.mblog.modules.entity.Nav;
import com.mtons.mblog.modules.entity.NavType;
import com.mtons.mblog.modules.service.NavService;
import com.mtons.mblog.web.controller.BaseController;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

@Controller("adminNavController")
@RequestMapping("/admin/nav")
public class NavController extends BaseController {
	
	@Autowired
	private NavService navService;

	@RequestMapping("/type/list")
	public String navTypeList(String name, @RequestParam(defaultValue = "true")Boolean nav, HttpServletRequest request, ModelMap model) {
		Pageable pageable = wrapPageable(Sort.by(Sort.Direction.DESC, "weight"));
		Page<NavTypeVO> page = navService.navTypeList(name, pageable, nav);
		model.addAttribute("result", page);
		return "/admin/nav/nav_type";
	}
	
	@PostMapping("/type/delete")
	@ResponseBody
	public Result<Object> navTypeDelete(@RequestParam("id")Integer id) {
		if (id != null) {
			navService.navTypeDelete(id);
			return Result.success();
		}
		return Result.failure("id为空");
	}
	
	@PostMapping("/delete")
	@ResponseBody
	public Result<Object> navDelete(@RequestParam("id")Integer id) {
		if (id != null) {
			navService.navDelete(id);
			return Result.success();
		}
		return Result.failure("id为空");
	}
	
	@PostMapping("/type/weight")
	@ResponseBody
	public Result<Object> navTypeWeight(Integer id) {
		if (id != null) {
			navService.navTypeWeight(id);
			return Result.success();
		}
		return Result.failure("id为空");
	}
	
	@PostMapping("/weight")
	@ResponseBody
	public Result<Object> navWeight(Integer id, Integer typeId) {
		if (id != null) {
			navService.navWeight(id, typeId);
			return Result.success();
		}
		return Result.failure("id为空");
	}
	
	@PostMapping("/type/update")
	public String navTypeUpdate(NavType view) {
		if (view != null) {
			navService.navTypeUpdate(view);
		}
		return "redirect:/admin/nav/type/list?nav=false";
	}
	
	@PostMapping("/update")
	public String navUpdate(Nav view) {
		if (view != null) {
			navService.navUpdate(view);
		}
		return "redirect:/admin/nav/list";
	}
	
	@GetMapping("/type/view")
	public String navTypeView(Integer id, HttpServletRequest request, ModelMap model) {
		if (id != null) {
			NavTypeVO type = navService.getById(id);
			model.put("view", type);
		}
		return "/admin/nav/nav_type_view";
	}
	
	@GetMapping("/view")
	public String navView(Integer id, HttpServletRequest request, ModelMap model) {
		List<NavType> list = navService.findAll();
		model.addAttribute("types", list);
		if (id != null) {
			Nav nav = navService.getNavById(id);
			model.put("view", nav);
		}
		return "/admin/nav/nav_view";
	}
	
	@RequestMapping("/list")
	public String navList(String name, Integer typeId, HttpServletRequest request, ModelMap model) {
		Pageable pageable = wrapPageable(Sort.by(Sort.Direction.DESC, "weight"));
		Page<NavVO> page = navService.navList(name, typeId, pageable);
		List<NavType> list = navService.findAll();
		model.addAttribute("result", page);
		model.addAttribute("name", name);
		model.addAttribute("typeId", typeId);
		model.addAttribute("types", list);
		return "/admin/nav/nav";
	}
}
